
#include <system.h>
#include <capture.h>
#include <display.h>
#include <audio.h> // AYK - 0728

#define AUDIO
#define DISPLAY

int audioPlaybackEnable = 1;
int UI_sysStart(int mode, int videoInterface, int videoSystem, int audioChan)
{
	int status;
	CAPTURE_CreatePrm captureCreatePrm;
	
#ifdef AUDIO	
	CAPTURE_Info *pCaptureInfo;
#endif
#ifdef DISPLAY	
	DISPLAY_CreatePrm displayCreatePrm;  
#endif	
#ifdef AUDIO	
	AUDIO_CreatePrm audioCreatePrm;
#endif

	OSA_printf(" MCVIP: Starting. Please wait !!!\n");

	captureCreatePrm.videoDecoderMode = mode;
	captureCreatePrm.videoSystem      = videoSystem;
	captureCreatePrm.videoIfMode      = videoInterface;

	OSA_printf(" MCVIP: Capture Starting !!!\n");
	status = CAPTURE_create(&captureCreatePrm);
	if(status!=OSA_SOK) 
	{
		OSA_ERROR("CAPTURE_create()\r\n");
		return status;
	}

#ifdef DISPLAY	
	displayCreatePrm.displayMode = DISPLAY_MODE_COMPOSITE;
	displayCreatePrm.captureInfo = CAPTURE_getInfo();
	OSA_printf(" MCVIP: Display Starting !!!\n");
	status = DISPLAY_create(&displayCreatePrm);
	if(status!=OSA_SOK) 
	{
		OSA_ERROR("DISPLAY_create()\r\n");
		CAPTURE_delete();
		return status;
	}
#endif

#ifdef AUDIO	
	pCaptureInfo = CAPTURE_getInfo();
	if(audioChan > 0)
	{
		audioCreatePrm.audPlayCh   = audioChan - 1; //Convert external 1..4 to internal 0..3
		audioCreatePrm.pMcvipHndl  = pCaptureInfo->mcvipHndl;
		audioCreatePrm.pCreatePrm  = &(pCaptureInfo->createPrm);

		if(audioPlaybackEnable == 1)
		{
			audioCreatePrm.pCreatePrm->enablePlayback = 1;
		}

		audioCreatePrm.pCreatePrm->format       = AFMT_S16_LE;
		audioCreatePrm.pCreatePrm->numChannels  = 2;
		audioCreatePrm.pCreatePrm->samplingRate = 16000;

		status = AUDIO_create(&audioCreatePrm);

		OSA_printf(" MCVIP: Audio Starting !!!\n");
		if(status != OSA_SOK)
		{
			OSA_ERROR("AUDIO_create()\r\n");
			CAPTURE_delete();
			DISPLAY_delete();
			return status;
		}
	}
#endif

#ifdef DISPLAY	
	status = DISPLAY_start();  
	if(status!=OSA_SOK) 
	{
		OSA_ERROR("DISPLAY_start()\r\n");
		DISPLAY_delete();    
		CAPTURE_delete();
		if(audioChan > 0)
		{
			AUDIO_delete(); // AYK - 0728
		}
		return status;
	}  
#endif	

	status = CAPTURE_start();  
	if(status!=OSA_SOK) 
	{
		OSA_ERROR("CAPTURE_start()\r\n");
		DISPLAY_stop();
		DISPLAY_delete();        
		CAPTURE_delete();
		if(audioChan > 0)
		{
			AUDIO_delete(); // AYK - 0728
		}
		return status;
	}  
	
#ifdef AUDIO	
	if(audioChan > 0)
	{
		OSA_printf("MCVIP: Audio Starting!!!\n");

		status = AUDIO_start();
		if(status!=OSA_SOK)
		{
			OSA_ERROR("AUDIO_start()\r\n");
			DISPLAY_stop();
			CAPTURE_stop();
			DISPLAY_delete();
			CAPTURE_delete();
			AUDIO_delete();
			return status;
		}
	}
#endif

	OSA_printf(" MCVIP: Start DONE !!!\n");
	return status;
}

int UI_sysStop(int audioChan)
{
	OSA_printf(" MCVIP: Display Stopping !!!\n");
	DISPLAY_stop();

	OSA_printf(" MCVIP: Capture Stopping !!!\n");
	CAPTURE_stop();

	OSA_printf(" MCVIP: Capture Deleting !!!\n");
	CAPTURE_delete();

	OSA_printf(" MCVIP: Display Deleting !!!\n");
	DISPLAY_delete();

	if(audioChan > 0)
	{
		OSA_printf(" MCVIP: Audio Stopping !!!\n");
		AUDIO_stop();
		OSA_printf(" MCVIP: Audio Deleting !!!\n");
		AUDIO_delete();
	}

	OSA_printf(" MCVIP: Stopped !!!\n");
	return OSA_SOK;
}

char gUI_menuRunning[] = {
"\r\n =================="
"\r\n MCVIP Running Menu"
"\r\n =================="
"\r\n"
"\r\n 1: Save video frame to file"
"\r\n 2: Display next video page"
"\r\n 0: Stop and Exit"
"\r\n "
"\r\n Enter Choice : "
};

/*
"\r\n +: Increase audio volume"
"\r\n -: Decrease audio volume"
*/

int UI_menuRunning(int mode, int videoInterface, int videoSystem, int audioChan)
{
  char ch;
  Bool done = FALSE;
  int status;
  
  status = UI_sysStart(mode, videoInterface, videoSystem, audioChan);

  if(status!=OSA_SOK)
    return status;
 
  do 
  {
    OSA_printf(gUI_menuRunning);

    ch = SYSTEM_getInput();

    OSA_printf("\r\n");

    switch (ch) 
    {
      case '0':
        done = TRUE;
        break;
        
      case '1':
        CAPTURE_saveFrame();
        break;

      case '2':
        DISPLAY_nextPage();   
        break;  
	
 	    case '+':
      case '=':
        AUDIO_increaseVolume();
        break;

      case '-':
      case '_':
        AUDIO_decreaseVolume();
        break;
/*	
      case 'i':
        SYSTEM_profileInfoShow();
        break;
*/
  
	  default:
        break;
    }
  } while(!done);

  UI_sysStop(audioChan);

//  SYSTEM_profileInfoShow();

  return OSA_SOK;
}

char gUI_menuMain[] = {
"\r\n =============================="
"\r\n MCVIP Main Menu (%s Display)"
"\r\n =============================="
"\r\n"
"\r\n 01: Start 2-Ch D1          "
"\r\n 02: Start 4-Ch Half-D1     "
"\r\n 03: Start 4-Ch CIF         "
"\r\n 04: Start 8-Ch CIF         "
"\r\n 05: Start 4-Ch Half-D1 + D1"
"\r\n 06: Start 4-Ch CIF     + D1"
"\r\n 07: Start 8-Ch CIF     + D1"
"\r\n"
"\r\n 11: Start 2-Ch D1           Cropped"
"\r\n 12: Start 4-Ch Half-D1      Cropped"
"\r\n 13: Start 4-Ch CIF          Cropped"
"\r\n 14: Start 8-Ch CIF          Cropped"
"\r\n 15: Start 4-Ch Half-D1 + D1 Cropped"
"\r\n 16: Start 4-Ch CIF     + D1 Cropped"
"\r\n 17: Start 8-Ch CIF     + D1 Cropped"
"\r\n"
"\r\n 00: Exit"
"\r\n "
"\r\n Enter Choice : "
};

char gUI_menuAudio[] = {
"\r\n =================="
"\r\n MCVIP Audio CH select Menu"
"\r\n =================="
"\r\n"
"\r\n 0-f: Select audio channel (1-16)"
"\r\n"
"\r\n x: Stop and Exit"
"\r\n "
"\r\n Enter Choice : "
};

int UI_menuMain(int videoSys, int audioChan)
{
	char ch, ch_crop;
	Bool done = FALSE, run = TRUE;
	int mode = MCVIP_VIDEO_DECODER_MODE_4CH_CIF, videoIf = MCVIP_VIDEO_IF_MODE_BT656;
	char modeStr[8];
  
	if(videoSys==MCVIP_VIDEO_SYSTEM_NTSC)
	{ 
		strcpy(modeStr,"NTSC");
	}
	else
	{
		strcpy(modeStr,"PAL ");  
	}
 
	do 
	{
		OSA_printf(gUI_menuMain, modeStr);
		ch_crop = SYSTEM_getInput();
		ch = SYSTEM_getInput();
		OSA_printf("\r\n");
    
		if((ch_crop == '0') || (ch_crop == '1'))
		{
			done = TRUE;
			videoIf = MCVIP_VIDEO_IF_MODE_BT656;			
	    
			switch (ch) 
			{
				case '0':
					//Exit without running
					run = FALSE;
					goto EXIT;
					break;
				case '1':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_2CH_D1;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_2CH_D1_CROP;
					break;
				case '2':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1_CROP;
					break;
				case '3':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_CIF;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_CIF_CROP;
					break;
				case '4':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_8CH_CIF;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_8CH_CIF_CROP;
					break;
				case '5':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1_PLUS_D1;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1_PLUS_D1_CROP;
					break;
				case '6':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_CIF_PLUS_D1;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_4CH_CIF_PLUS_D1_CROP;
					break;
				case '7':
					if( ch_crop == '0' )
						mode = MCVIP_VIDEO_DECODER_MODE_8CH_CIF_PLUS_D1;
					else
						mode = MCVIP_VIDEO_DECODER_MODE_8CH_CIF_PLUS_D1_CROP;
					break;
				default:
					//2nd character (video format) was invalid - repeat menu
					done = FALSE;
					break;
			} //switch( ch )
		} //if((ch_crop == '0') || (ch_crop == '1'))
	}while(!done);

    if(run) 
    {
      UI_menuRunning(mode, videoIf, videoSys, audioChan);    
    }

EXIT:
  return OSA_SOK;
}

void showUsage(char *str)
{
  OSA_printf(" \n");
  OSA_printf(" Multi Channel Video Input Port (MCVIP) Capture Demo, (c) Texas Instruments 2009\n");
  OSA_printf(" \n");  
  OSA_printf(" USAGE: %s <Cascaded TVP5158s> <Video System> <TVP5158EVM I2C Master> <Audio Channel>\n", str);
  OSA_printf(" \n");  
  OSA_printf(" <Cascaded TVP5158s>     = 1 (default) or 2 \n");
  OSA_printf(" <Video System>          = NTSC (default) or PAL \n");
  OSA_printf(" <TVP5158EVM I2C Master> = DM6446 (default) or WinVCC \n");  
  OSA_printf(" <Audio Channel>         = 0 (No Audio - default) or 1-8 \n");
  OSA_printf(" \n");    
}

int orig_main(int argc, char **argv)	// CHANGE from main by jch
{
  int status, videoSys,numMCVIP;
  int debug = 0; 
  int audioChan = 0; //0=No audio or select chan 1-8
	int temp;
  
  showUsage(argv[0]);

  numMCVIP = 1;
  
  if(argc > 1) 
  {
    if(strcmp(argv[1], "2") == 0)
    {
      numMCVIP = 2;  
    }
  }

  videoSys = MCVIP_VIDEO_SYSTEM_NTSC; 
  
  if(argc > 2) 
  {  
    if(strcmp(argv[2], "PAL") == 0)
    {
      videoSys = MCVIP_VIDEO_SYSTEM_PAL;  
    }
  }

  if(argc > 3) 
  {
    if(strcmp(argv[3], "WinVCC") == 0) 
    {
      // run in debug mode, WinVCC controls TVP5158 I2C
      debug=1;
    }
  }

  audioChan = 0;
  if(argc > 4) 
  {
		temp = atoi( argv[4] );
    if( ( temp > 0 ) && ( temp <= 8 ) )
	  {
	 	  audioChan = temp;
	  }
  }

  status = SYSTEM_init(debug,numMCVIP,audioChan);

  if(status!=OSA_SOK)
    return status;
  
  UI_menuMain(videoSys,audioChan);
  
  OSA_printf(" MCVIP: System Exiting !!!\n");  
  SYSTEM_exit();
  OSA_printf(" MCVIP: Bye Bye !!!\n");  
  OSA_printf(" \n");

  return 0;
}





















